package com.luoxue.execsql.rest.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.luoxue.execsql.common.contact.ExecSqlContact;
import com.luoxue.execsql.rest.bean.base.controller.BaseController;
import com.luoxue.execsql.rest.entity.SqlSnippet;
import com.luoxue.execsql.rest.mapper.SqlSnippetMapper;
import com.luoxue.execsql.rest.service.ISqlSnippetService;
import org.apache.logging.log4j.util.Strings;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/sqlSnippet")
public class SqlSnippetController extends BaseController<SqlSnippet, SqlSnippetMapper, ISqlSnippetService> {
    public SqlSnippetController(ISqlSnippetService service) {
        super(service);
    }

    @Override
    protected void queryParams(SqlSnippet entity, QueryWrapper<SqlSnippet> queryWrapper) {
        if (Strings.isNotBlank(entity.getName())) {
            if (entity.getName().startsWith(ExecSqlContact.refineSearch)) {
                queryWrapper.lambda().eq(SqlSnippet::getName, entity.getName().substring(1));
            } else {
                queryWrapper.lambda().like(SqlSnippet::getName, entity.getName());
            }
        }
        queryWrapper.orderByDesc("update_time", "id");
    }
}
